from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework.permissions import IsAdminUser
from meiduo_admin.serializers.orders import OrderSerializer
from meiduo_admin.utils import PageNum
from orders.models import OrderInfo

class OrderView(ReadOnlyModelViewSet):
    queryset = OrderInfo.objects.all()
    serializer_class = OrderSerializer
    pagination_class = PageNum
    permission_classes = [IsAdminUser]
    #  重写获取查询机数据的方法

    def get_queryset(self):
        if self.request.query_params.get('keyword') == '':
            return  OrderInfo.objects.all()
        elif self.request.query_params.get('keyword') is None:
            return OrderInfo.objects.all()
        else:
            return OrderInfo.objects.filter(order_id__contains=self.request.query_params.get('keyword'))
    @action(methods = ['put'],detail=True)


    def status(self,request,pk):
        # 修改订单状态
        # 查询要修改的订单状态
        try:
            order = OrderInfo.objects.get(order_id=pk)
        except:
            return Response({'error':'订单标号错误'})
        # 修改订单状态,
        #获取订单状态
        status =  request.data.get('status')
        if status is None:
            return Response({'error':'缺少状态值'})
        order.status = status
        order.save()
        #返回修改信息
        return Response({
            'order_id':pk,
            'status':status
        })


